package com.itita.ww2.handler.requesthandler.building;

import java.sql.SQLException;

import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.itita.ww2.core.MessageHandler;
import com.itita.ww2.core.R;
import com.itita.ww2.core.WW2Exception;
import com.itita.ww2.enums.WW2ExceptionCode;
import com.itita.ww2.game.user.UserService;
import com.itita.ww2.game.user.UserBuildingManager.BuildingType;
import com.itita.ww2.model.user.UserProfile;
import com.itita.ww2.utils.handler.IRequestOper;

/**
 * 基地建筑解锁
 * @author NorthLan
 *
 */
public class BaseBDUnlock implements IRequestOper {

	@Override
	public boolean execute(String requestCMD, User user, ISFSObject params) throws WW2Exception, SQLException {
		UserProfile userProfile = UserService.getInstance().getUserProfile(user);
		String bdCode = params.getUtfString("code");
		BuildingType bdType = BuildingType.codeValueOf(bdCode);

		ISFSObject backObj = SFSObject.newInstance();
		backObj.putInt(R.DATA_KEY_COUNTER, params.getInt(R.DATA_KEY_COUNTER));

		// 解锁,同时扣除资源
		if (!userProfile.getBDManager().unlockBuilding(bdType)) {
			throw new WW2Exception(WW2ExceptionCode.INVALID_OPT, backObj);
		}

		backObj.putUtfString("Unlock", bdCode);

		// 新手引导设置一下
		if (params.containsKey("guidestep")) {
			userProfile.getUserInfo().setGuideStep(params.getInt("guidestep"));
		}

		MessageHandler.getInstance().sendMsg(requestCMD, backObj, user, null, true);
		return true;
	}

}
